<template>
  <div class='container'>

    <el-dialog :title="subjectTitle" :visible="isShow" @close="handleColse" class="dialog">
      <el-form ref="subForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="form.subjectName" placeholder="请输入学科名称" />
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch
            v-model="form.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer class="dialog-footer">
        <el-button @click="handleColse">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/subjects.js'
export default {
  name: 'SubjectsAdd',
  data () {
    return {
      form: {
        subjectName: '',
        isFontDisplay: '1'
      },
      rules: {
        subjectName: [
          { required: true, message: '学科名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 30, message: '学科名称只能是1-15个字符', trigger: ['blur', 'change'] }
        ]
      }

    }
  },
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    subjectTitle () {
      return this.form.id ? '修改学科' : '新增学科'
    }
  },
  methods: {
    handleColse () {
      this.$emit('update:isShow', false)
      this.$refs.subForm.resetFields()
      this.form = {
        subjectName: '',
        isFontDisplay: 1
      }
    },
    handleSubmit () {
      this.$refs.subForm.validate(async (flag) => {
        if (!flag) return
        if (this.form.id) {
          await update(this.form)
          this.$message.success('修改学科成功')
        } else {
          await add(this.form)
          this.$message.success('添加学科成功')
        }
        this.handleColse()
        this.$emit('refresh')
      })
    }
  }

}
</script>

<style scoped lang='scss'>
    .dialog {
      ::v-deep {

      .el-dialog {
        width: 400px;

        .el-dialog__footer {
            text-align: right;
         }
      }
    }
    }
</style>
